<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="购买人" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入购买人"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="orderId">
        <el-input
          v-model="queryParams.ConsigneeCode"
          placeholder="请输入购买人证件号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="push_inventory">
        <el-select v-model="queryParams.pushOrder" placeholder="订单状态" clearable size="small">
          <el-option
            v-for="(dict,index) in PushOrderOptions"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单时间" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="pushBatch(1)"
          >批量申报</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="pushBatch(2)"
          >批量变更</el-button>
        </el-col>
      </el-col></el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单时间" align="center" prop="orderDate" />
      <el-table-column label="订购人" align="center" prop="buyerName" />
      <!--      <el-table-column label="交易流水号" align="center" prop="payOrderNo" />-->
      <el-table-column label="订购人证件号" align="center" prop="ConsigneeCode" :show-overflow-tooltip="true" />
      <el-table-column label="订单金额" align="center" prop="realPayment" />
      <el-table-column label="清单状态" align="center" :formatter="statusFormat" />
      <el-table-column label="详情" align="center">
        <template v-slot="scope">
          <span style="color: #20a0ff;cursor:pointer" @click="see(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template v-slot="scope">
          <el-button
            v-if="scope.row.push_inventory === 0 || scope.row.push_inventory === 100"
            size="mini"
            type="primary"
            @click="pushOne(scope.row.orderId, 1)"
          >申报</el-button>
          <el-button
            v-if="scope.row.push_inventory !== 0 && scope.row.push_inventory !== 100"
            size="mini"
            type="success"
            @click="pushOne(scope.row.orderId, 2)"
          >变更</el-button>
          <el-button
            size="mini"
            type="warning"
            :disabled="scope.row.push_inventory === 0 ? true : false"
            @click="getReceipt(scope.row)"
          >查询回执</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 查看详情 -->
    <OrderDetails :show.sync="show" :form="form" />
  </div>
</template>

<script>
import { list, orderInfo, taizhouPushClearOrders } from '@/api/order'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      ids: [], // 选中项
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      // 表单参数
      form: {},
      info: '',
      // 表单校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      },
      // 订单状态
      PushOrderOptions: [
        { 'value': 0, 'label': '未推送' },
        { 'value': 1, 'label': '电子口岸已暂存' },
        { 'value': 2, 'label': '电子口岸申报中' },
        { 'value': 3, 'label': '发送海关成功' },
        { 'value': 4, 'label': '发送海关失败' },
        { 'value': 100, 'label': '海关退单' },
        { 'value': 120, 'label': '海关入库' },
        { 'value': 300, 'label': '海关入库' },
        { 'value': 399, 'label': '海关审结' },
        { 'value': 800, 'label': '放行' },
        { 'value': 899, 'label': '结关' },
        { 'value': 500, 'label': '查验' },
        { 'value': 501, 'label': '扣留移送通关' },
        { 'value': 502, 'label': '扣留移送缉私' },
        { 'value': 503, 'label': '扣留移送法规' },
        { 'value': 599, 'label': '其它扣留' },
        { 'value': 700, 'label': '退运' }

      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        buyerName: undefined,
        // 日期范围
        orderDate: [],
        pushOrder: undefined
      },
      show: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      list(this.queryParams).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 查询订单详情 */
    see(row) {
      this.loading = true
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        this.loading = false
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.push_inventory
      for (const d of this.PushOrderOptions) {
        if (row.push_inventory === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** 单个推送 type:1 申报  2：变更申报 */
    pushOne(orderId, type) {
      const that = this
      that.$confirm('是否' + (type === 1 ? '申报' : '变更') + orderId + '订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        // 批量推送
        that.push([orderId], type)
      }).catch(() => {})
    },
    /** 批次推送 type:1 申报  2：变更申报 */
    pushBatch(type) {
      const that = this
      if (that.ids.length === 0) {
        that.$message.error('请选择订单')
        return
      }
      // 判断是否可申报/变更
      let flag = false
      for (let i = 0, ids = that.ids; i < ids.length; i++) {
        for (let j = 0, list = that.list; j < list.length; j++) {
          if (type === 1 && list[j].orderId === ids[i] && (list[j].push_inventory !== 0 && list[j].push_inventory !== 100)) {
            that.$message.error('订单' + list[j].orderId + '当前状态不能申报操作')
            flag = true
            break
          } else if (type === 2 && list[j].orderId === ids[i] && (list[j].push_inventory === 0 || list[j].push_inventory === 100)) {
            that.$message.error('订单' + list[j].orderId + '当前状态不能变更操作')
            flag = true
            break
          }
        }
        if (flag) {
          break
        }
      }
      if (flag) {
        return false
      }

      that.$confirm('是否' + (type === 1 ? '申报' : '变更') + that.ids[0] + '等订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        // 批量推送
        that.push(that.ids, type)
      }).catch(() => {})
    },
    /** 订单 type:1 申报  2：变更申报 */
    push(orders, type) {
      const that = this
      that.loading = true
      taizhouPushClearOrders({ orders: orders, type: type }).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.getList()
        }
        that.loading = false
      })
    },
    /** 查询回执 */
    getReceipt(row) {
      this.$message.info('暂不支持回执查询')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        buyerName: undefined,
        startTime: undefined,
        endTime: undefined,
        orderDate: []
      }
      this.handleQuery()
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.startTime = value[0]
        this.queryParams.endTime = value[1]
      } else {
        this.queryParams.startTime = undefined
        this.queryParams.endTime = undefined
      }
    },
    /** 选中的项 */
    handleSelectionChange(val) {
      const ids = []
      val.map((item, index, val) => {
        ids.push(item.orderId)
      })
      this.ids = ids
    }
  }
}
</script>
